package com.gnerv.plug.maven;

import com.gnerv.plug.constant.EmbraceSourceInfo;
import com.gnerv.plug.maven.action.RefreshDependencyAction;
import com.gnerv.plug.maven.model.DependencyInfoList;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class MavenGroupAction extends ActionGroup {

    public MavenGroupAction() {
        super();
    }

    public MavenGroupAction(@Nullable String shortName, boolean popup) {
        super(shortName, popup);
    }

    @NotNull
    @Override
    public AnAction[] getChildren(AnActionEvent e) {
        final VirtualFile file = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE);
        String name = file.getName();
        if (EmbraceSourceInfo.FILE_NAME_POM.equals(name)) {
            AnAction[] anActions = new AnAction[DependencyInfoList.DEPENDENCYINFOS.size() + 1];
            anActions[0] = new RefreshDependencyAction("RefreshDependency", "RefreshDependency", null);
            for (int i = 0; i < DependencyInfoList.DEPENDENCYINFOS.size(); i++) {
                anActions[i + 1] = new DependencyGroupAction(DependencyInfoList.DEPENDENCYINFOS.get(i).getArtifactId(), true, DependencyInfoList.DEPENDENCYINFOS.get(i));
            }
            return anActions;
        }
        return new AnAction[]{};
    }

}
